package com.esri.core.geometry;

import com.esri.core.geometry.AttributeStreamOfInt32;
import com.esri.core.geometry.Geometry;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public final class ScanlineRasterizer {
    private AttributeStreamOfInt64 a;
    private AttributeStreamOfInt64 b;
    private AttributeStreamOfInt32 c;
    private AttributeStreamOfInt32 d;
    private AttributeStreamOfInt32 e;
    private int[] f;
    private int h;
    private Transformation2D i;
    private Envelope2D j;
    private int[] l;
    private int n;
    private ScanCallback v;
    private int s = 134217727;
    private double t = 134217727;
    private int o = 0;
    private int p = 0;
    private int r = 0;
    private int q = 0;
    private int w = 0;
    private int x = 0;
    private int m = -1;
    private boolean u = false;
    private int g = 0;
    private Envelope2D k = new Envelope2D();

    /* loaded from: classes.dex */
    public static abstract class ScanCallback {
        public abstract void flush(int[] iArr, int i);
    }

    private int a(long j) {
        return (int) ((j + 1073741824) >> 31);
    }

    public void setRenderingSurface(int i, int i2, ScanCallback scanCallback) {
        this.w = i;
        this.x = i2;
        this.v = scanCallback;
        double d = i;
        Double.isNaN(d);
        double d2 = i2;
        Double.isNaN(d2);
        this.j = new Envelope2D(-0.5d, -0.5d, d - 0.5d, d2 - 0.5d);
        this.k = new Envelope2D(NumberUtils.negativeInf(), this.j.ymin, NumberUtils.positiveInf(), this.j.ymax);
    }

    private void a() {
        int[] iArr = this.l;
        if (iArr == null || iArr.length < this.x) {
            this.l = new int[this.x];
            for (int i = 0; i < this.x; i++) {
                this.l[i] = -1;
            }
        }
        if (this.u) {
            return;
        }
        this.u = true;
        AttributeStreamOfInt64 attributeStreamOfInt64 = new AttributeStreamOfInt64(0);
        this.a = attributeStreamOfInt64;
        attributeStreamOfInt64.reserve(256);
        AttributeStreamOfInt64 attributeStreamOfInt642 = new AttributeStreamOfInt64(0);
        this.b = attributeStreamOfInt642;
        attributeStreamOfInt642.reserve(256);
        AttributeStreamOfInt32 attributeStreamOfInt32 = new AttributeStreamOfInt32(0);
        this.c = attributeStreamOfInt32;
        attributeStreamOfInt32.reserve(256);
        AttributeStreamOfInt32 attributeStreamOfInt322 = new AttributeStreamOfInt32(0);
        this.d = attributeStreamOfInt322;
        attributeStreamOfInt322.reserve(256);
    }

    private void a(long j, long j2, int i, int i2, int i3, boolean z) {
        int size = this.a.size();
        int i4 = this.h;
        if (size == i4) {
            this.a.add(j);
            this.b.add(j2);
            this.c.add(i);
            this.c.add(i2);
            this.d.add(0);
            b(this.h, i3, z ? 1 : 0);
        } else {
            this.a.write(i4, j);
            this.b.write(this.h, j2);
            this.c.write(this.h * 2, i);
            this.c.write((this.h * 2) + 1, i2);
            b(this.h, i3, z ? 1 : 0);
        }
        this.h++;
    }

    private void a(Point2D point2D, Point2D point2D2) {
        int i;
        Point2D point2D3 = new Point2D(point2D.x, point2D.y);
        Point2D point2D4 = new Point2D(point2D2.x, point2D2.y);
        boolean z = point2D3.y > point2D4.y;
        if (z) {
            double d = point2D3.x;
            double d2 = point2D3.y;
            point2D3.setCoords(point2D4.x, point2D4.y);
            point2D4.setCoords(d, d2);
        }
        this.i.a(point2D3, point2D3);
        this.i.a(point2D4, point2D4);
        if (point2D3.y < this.j.ymin) {
            if (point2D4.y < this.j.ymin) {
                return;
            }
        } else if (point2D3.y > this.j.ymax && point2D4.y > this.j.ymax) {
            return;
        }
        if (this.k.a(point2D3, point2D4) == 0) {
            return;
        }
        if (point2D3.x < this.j.xmin && point2D4.x < this.j.xmin) {
            point2D3.x = this.j.xmin - 1.0d;
            point2D4.x = this.j.xmin - 1.0d;
        } else if (point2D3.x > this.j.xmax && point2D4.x > this.j.xmax) {
            point2D3.x = this.j.xmax + 1.0d;
            point2D4.x = this.j.xmax + 1.0d;
        }
        double d3 = point2D3.x;
        double d4 = this.t;
        point2D3.x = NumberUtils.snap(d3, -d4, d4);
        double d5 = point2D4.x;
        double d6 = this.t;
        point2D4.x = NumberUtils.snap(d5, -d6, d6);
        long a = a(point2D3.x);
        a(point2D3.y);
        a(point2D4.x);
        a(point2D4.y);
        int round = (int) Math.round(point2D3.y);
        int round2 = (int) Math.round(point2D4.y);
        if (round == round2) {
            return;
        }
        a();
        this.o = Math.min(round, this.o);
        this.p = Math.max(round2, this.p);
        this.o = NumberUtils.a(this.o, 0, this.w - 1);
        this.p = NumberUtils.a(this.p, 0, this.w - 1);
        this.q = Math.min((int) Math.round(point2D3.x), this.q);
        this.q = Math.min((int) Math.round(point2D4.x), this.q);
        this.r = Math.max((int) Math.round(point2D3.x), this.r);
        this.r = Math.max((int) Math.round(point2D4.x), this.r);
        long a2 = a((point2D4.x - point2D3.x) / (point2D4.y - point2D3.y));
        int i2 = round2 - round;
        int[] iArr = this.l;
        int i3 = iArr[round];
        int i4 = this.h;
        iArr[round] = i4;
        if (i3 < 0) {
            i = -1;
        } else {
            b(i3, i4);
            i = i3;
        }
        a(a, a2, -1, i, i2, z);
    }

    private void b() {
        this.o = this.x + 1;
        this.p = -1;
        this.q = this.w + 1;
        this.r = -1;
        this.m = -1;
        this.h = 0;
        this.n = 0;
    }

    private void a(Point2D[] point2DArr, int i) {
        if (i < 3) {
            return;
        }
        b();
        Point2D point2D = point2DArr[0];
        Point2D point2D2 = new Point2D(point2D.x, point2D.y);
        int i2 = 1;
        while (i2 < i) {
            Point2D point2D3 = new Point2D(point2DArr[i2].x, point2DArr[i2].y);
            a(point2D2, point2D3);
            i2++;
            point2D2 = point2D3;
        }
        a(point2D2, point2D);
    }

    private void a(MultiPathImpl multiPathImpl) {
        b();
        SegmentIteratorImpl querySegmentIterator = multiPathImpl.querySegmentIterator();
        while (querySegmentIterator.nextPath()) {
            while (querySegmentIterator.hasNextSegment()) {
                Segment nextSegment = querySegmentIterator.nextSegment();
                if (nextSegment.getType() != Geometry.Type.LINE) {
                    throw new GeometryException("internal error");
                }
                a(nextSegment.a(), nextSegment.j());
            }
        }
    }

    private boolean c() {
        int i = this.m;
        boolean z = false;
        if (i == -1) {
            return false;
        }
        while (i >= 0) {
            int e = e(i);
            int f = f(i);
            if (j(i)) {
                if (e > -1 && b(e) > b(i)) {
                    z = true;
                }
            } else {
                if (e >= 0) {
                    c(e, f);
                }
                if (f >= 0) {
                    b(f, e);
                }
                if (i == this.m) {
                    this.m = f;
                }
                this.n--;
            }
            i = f;
        }
        return z;
    }

    private void a(int i) {
        int i2 = 0;
        if (this.e == null) {
            this.e = new AttributeStreamOfInt32(0);
        }
        this.e.a(false);
        int[] iArr = this.l;
        int i3 = iArr[i];
        iArr[i] = -1;
        int i4 = 0;
        while (i3 >= 0) {
            this.e.add(i3);
            i4++;
            i3 = f(i3);
        }
        AttributeStreamOfInt32 attributeStreamOfInt32 = this.e;
        attributeStreamOfInt32.Sort(0, attributeStreamOfInt32.size(), new ScanlineRasterizerEdgeComparer());
        int i5 = this.m;
        if (i5 >= 0) {
            while (i2 < i4) {
                int read = this.e.read(i2);
                if (b(read) < b(i5)) {
                    int e = e(i5);
                    b(i5, read);
                    c(read, i5);
                    b(read, e);
                    if (e == -1) {
                        this.m = read;
                    } else {
                        c(e, read);
                    }
                    i2++;
                } else {
                    int f = f(i5);
                    if (f == -1) {
                        break;
                    }
                    i5 = f;
                }
            }
        }
        while (i2 < i4) {
            int read2 = this.e.read(i2);
            if (i5 >= 0) {
                c(i5, read2);
            } else {
                this.m = read2;
            }
            b(read2, i5);
            c(read2, -1);
            i2++;
            i5 = read2;
        }
        this.n += i4;
    }

    private void d() {
        int i = this.m;
        if (this.e == null) {
            this.e = new AttributeStreamOfInt32(0);
        }
        this.e.resize(f());
        int i2 = 0;
        while (i >= 0) {
            this.e.write(i2, i);
            i = f(i);
            i2++;
        }
        this.e.Sort(0, i2, new ScanlineRasterizerEdgeComparer());
        int read = this.e.read(0);
        this.m = read;
        b(read, -1);
        int i3 = 1;
        while (i3 < i2) {
            int read2 = this.e.read(i3);
            b(read2, read);
            c(read, read2);
            i3++;
            read = read2;
        }
        c(read, -1);
    }

    private void e() {
        int i = this.g;
        if (i == 0) {
            return;
        }
        this.v.flush(this.f, i);
        this.g = 0;
    }

    private void a(int i, int i2, int i3) {
        if (this.f == null) {
            this.f = new int[150];
        }
        int a = NumberUtils.a(i, 0, this.w - 1);
        int a2 = NumberUtils.a(i2, 0, this.w - 1);
        int[] iArr = this.f;
        int i4 = this.g;
        iArr[i4 * 3] = a;
        iArr[(i4 * 3) + 1] = a2;
        iArr[(i4 * 3) + 2] = i3;
        int i5 = i4 + 1;
        this.g = i5;
        if (i5 * 3 == iArr.length) {
            e();
        }
    }

    private void a(int i, boolean z) {
        int i2 = this.m;
        if (i2 == -1) {
            return;
        }
        int i3 = -this.s;
        int i4 = 0;
        boolean z2 = false;
        while (i2 >= 0) {
            int a = a(b(i2));
            if (z) {
                i4 += g(i2) != 0 ? 1 : -1;
            } else {
                i4 = i4 == 0 ? 1 : 0;
            }
            if (i4 == 0) {
                if (z2 && i3 < a) {
                    a(i3, a, i);
                }
                z2 = false;
            } else {
                if (!z2) {
                    i3 = a;
                }
                z2 = true;
            }
            i2 = f(i2);
        }
    }

    public void fillMultiPath(MultiPathImpl multiPathImpl, boolean z) {
        this.g = 0;
        this.h = 0;
        a(multiPathImpl);
        a(z);
    }

    private void a(boolean z) {
        for (int i = this.o; i <= this.p; i++) {
            if (c()) {
                d();
            }
            if (this.l[i] >= 0) {
                a(i);
            }
            if (this.m >= 0) {
                a(i, z);
            }
        }
        e();
    }

    public void fillConvexPolygon(Point2D[] point2DArr, int i) {
        this.g = 0;
        this.h = 0;
        a(point2DArr, i);
        a(false);
    }

    public void fillPoints(MultiPointImpl multiPointImpl, double d) {
        throw new GeometryException("internal error");
    }

    private int f() {
        return this.n;
    }

    private long b(int i) {
        return this.a.read(i);
    }

    private void a(int i, long j) {
        this.a.write(i, j);
    }

    private void c(int i) {
        AttributeStreamOfInt64 attributeStreamOfInt64 = this.a;
        attributeStreamOfInt64.write(i, attributeStreamOfInt64.read(i) + this.b.read(i));
    }

    private long d(int i) {
        return this.b.read(i);
    }

    private void b(int i, long j) {
        this.b.write(i, j);
    }

    private int e(int i) {
        return this.c.read(i * 2);
    }

    private void b(int i, int i2) {
        this.c.write(i * 2, i2);
    }

    private int f(int i) {
        return this.c.read((i * 2) + 1);
    }

    private void c(int i, int i2) {
        this.c.write((i * 2) + 1, i2);
    }

    private int g(int i) {
        return this.d.read(i) & 1073741823;
    }

    private int h(int i) {
        return this.d.read(i) & 1073741824;
    }

    private void b(int i, int i2, int i3) {
        this.d.write(i, i2 | (i3 << 30));
    }

    private boolean i(int i) {
        int read = this.d.read(i);
        int i2 = 1073741824 & read;
        int i3 = (read & 1073741823) - 1;
        if (i3 == 0) {
            return false;
        }
        this.d.write(i, i3 | i2);
        return true;
    }

    private boolean j(int i) {
        if (i(i)) {
            c(i);
            return true;
        }
        return false;
    }

    private long a(double d) {
        return Math.round(d * 2.147483648E9d);
    }

    public void setTransform(Transformation2D transformation2D) {
        this.i = transformation2D;
    }

    int a(int i, int i2) {
        long read = this.a.read(i);
        long read2 = this.a.read(i2);
        if (read < read2) {
            return -1;
        }
        return read > read2 ? 1 : 0;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class ScanlineRasterizerEdgeComparer extends AttributeStreamOfInt32.IntComparator {
        ScanlineRasterizerEdgeComparer() {
        }

        @Override // com.esri.core.geometry.AttributeStreamOfInt32.IntComparator
        public int compare(int i, int i2) {
            long read = ScanlineRasterizer.this.a.read(i);
            long read2 = ScanlineRasterizer.this.a.read(i2);
            if (read < read2) {
                return -1;
            }
            return read > read2 ? 1 : 0;
        }
    }
}
